const { withXcodeProject } = require('@expo/config-plugins')

const plugin = (config, options = {}) => {
  return withXcodeProject(config, async (config) => {
    const xcodeProject = config.modResults

    const bundleReactNativeCodeAndImagesBuildPhase = xcodeProject.buildPhaseObject(
      'PBXShellScriptBuildPhase',
      'Bundle React Native code and images'
    )

    if (!bundleReactNativeCodeAndImagesBuildPhase) {
      return config
    }

    const originalScript = JSON.parse(bundleReactNativeCodeAndImagesBuildPhase.shellScript)
    const patchedScript = removeExpoDefaultsFromBundleReactNativeShellScript(originalScript)
    bundleReactNativeCodeAndImagesBuildPhase.shellScript = JSON.stringify(patchedScript)

    return config
  })
}

/**
 * The Expo templates defaults the `CLI_PATH` and `BUNDLE_COMMAND` environment variables to use Expo CLI with the `export:embed` command.
 *
 * But in a VxRN project, users should be using `react-native.config.[c]?js` to override the `react-native bundle` command to use vxrn. So we should remove these Expo defaults, so that react-native-xcode.sh will use its default `react-native bundle` command.
 *
 * This is what we expect to be removed:
 *
 * ```sh
 * if [[ -z "$CLI_PATH" ]]; then
 *   # Use Expo CLI
 *   export CLI_PATH="$("$NODE_BINARY" --print "require.resolve(\'@expo/cli\', { paths: [require.resolve(\'expo/package.json\')] })")"
 * fi
 * if [[ -z "$BUNDLE_COMMAND" ]]; then
 *   # Default Expo CLI command for bundling
 *   export BUNDLE_COMMAND="export:embed"
 * fi
 * ```
 */
function removeExpoDefaultsFromBundleReactNativeShellScript(input) {
  // Regular expression to match the CLI_PATH block
  const cliPathRegex = /if \[\[ -z "\$CLI_PATH" \]\]; then[\s\S]*?fi\n?/g
  // Regular expression to match the BUNDLE_COMMAND block
  const bundleCommandRegex = /if \[\[ -z "\$BUNDLE_COMMAND" \]\]; then[\s\S]*?fi\n?/g

  // Remove both sections from the input string
  return input.replace(cliPathRegex, '').replace(bundleCommandRegex, '')
}

module.exports = plugin
